import org.omg.PortableInterceptor.INACTIVE;

import javax.management.relation.RelationNotFoundException;

import java.util.*;

/**
 * @User: vitobo
 * @Date: 2024-07-01
 * @Description: map set 学习
 */
class Student{

}
class Node{
    public static  int val;

    public Node next;
    public Node random;

    public Node(int val) {
        this.val = val;
    }
}

public class Test {
    // 链表复制
    public Node copyRandomList(Node head){
        HashMap<Node, Node> map = new HashMap<>();
        // 1.遍历原来的链表,存储对应关系
        Node cur = head;
        while(cur != null){
            Node node = new Node(cur.val);
            map.put(cur,node);
            cur = cur.next;
        }

        cur = head;
        // 2.第2次遍历链表,修改next, random
        while (cur != null){
            map.get(cur).next = map.get(cur.next);
            map.get(cur).random = map.get(cur.random);
            cur = cur.next;
        }
        return map.get(head);
    }

    // 第一次出现的字符
    public int firstUniqChar(String s){
        int[] array = new int[26];
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            array[ch - 'a']++;
        }

        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if(array[ch-'a'] == 1){
                return i;
            }
        }
        return -1;
    }

    // 1.统计10w个数据当中, 不重复的数字
    public static void func1(int[] array){
        TreeSet<Integer> set = new TreeSet<>();
        for (int i = 0; i < array.length; i++) {
            set.add(array[i]);
        }
        System.out.println(set);
    }

    // 2.统计10w个数据当中,第一个重复的数据
    public static void func2(int[] array){
        HashSet<Integer> set = new HashSet<>();
        for (int i = 0; i < array.length; i++) {
            if(!set.contains(array[i])){
                set.add(array[i]);
            }else {
                System.out.println(array[i]);
                return;
            }
        }
    }

    // 3.统计10w个数据当中,每个数据出现的次数, 对应的关系
    public static void func3(int[] array){
        HashMap<Integer, Integer> map = new HashMap<>();
        // 1.遍历原来的数据,统计每个数据出现的次数
        for (int i = 0; i < array.length; i++) {
            int key = array[i];
            if(map.get(key) == null){
                map.put(key, 1);
            }else {
                int val = map.get(key);
                map.put(key, val+1);
            }
        }

        for(Map.Entry<Integer, Integer> entry : map.entrySet()){
            System.out.println("key: " + entry.getKey() + " 出现了: "+ entry.getValue());
        }
    }

    public static void main(String[] args) {
        int[] array = {1,2,3,1,2,3,2,4,1,2,3,1};
        func3(array);
    }

    public static void main4(String[] args) {
        int[] array = new int[10_0000];
        Random random = new Random();
        for (int i = 0; i < 10_0000; i++) {
            array[i] = random.nextInt(500);
        }
        func1(array);
    }

    public static void main3(String[] args) {
        HashMap<String, Integer> map = new HashMap<>();
        map.put("张三", 4);
        map.put("李四", 5);
        map.put(null, null);
        System.out.println(map.get(null));

        Set<Map.Entry<String,Integer>> entries = map.entrySet();
        for(Map.Entry<String,Integer> entry : entries) {
            System.out.println(entry.getKey()+" -> " +entry.getValue());
        }
        System.out.println(map);

        HashMap<Student,Integer> map2 = new HashMap<>();
        map2.put(new Student(),2);
        map2.put(new Student(),1);
        System.out.println(map2);

        HashSet<Integer> set = new HashSet<>();
        set.add(1);
        set.add(1);
        set.add(1);
        set.add(1);
        System.out.println("size: "+ set.size());
    }

    public static void main2(String[] args) {
        TreeSet<String> set = new TreeSet<>();
        set.add("hello");
        set.add("world");
        TreeSet<Student> set1 = new TreeSet<>();
        set1.add(new Student());

        System.out.println(set.contains("hello"));
    }

    public static void main1(String[] args) {
        TreeMap<String, Integer> map = new TreeMap<>();
        // TreeMap当中, 不可以存放key为null
        map.put(null, 10);


    }
}
